//mystore.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAll:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that=this;
    wx.request({
      url: 'https://soya.91soya.com/api/content/' + app.globalData.openId,
      method:"get",
      success:function(res){
        var resp = res.data;
        var a = [];
        if (resp.length!=0){
          for (var i = 0; i < resp.length; i++) {
            a[i] = { id: i, pid: resp[i].id, pImg: " https://soya.91soya.com/uploads/" + resp[i].p_img, pName: resp[i].pro_name, storeImg: "/imgs/storeAc.png", prcie: resp[i].price, yPrice: resp[i].y_price, }
          }
          that.setData({
            pInfo: a
          })
        }
      }
    })
  },
  store:function(e){
    var that = this;
    if (app.globalData.openId) {
      wx.request({
        url: 'https://soya.91soya.com/api/coll/',
        method: "POST",
        // 传产品id
        data: {
          open_id: app.globalData.openId,
          pro_id: e.target.dataset.pid,
        },
        success: function (res) {
          if (res.data == 300) {
            that.setData({
              //  收藏成功这边改变一下样式
              ['pInfo[' + e.target.id+'].storeImg']: "/imgs/storeAc.png",
            })
          } else {
            that.setData({
              //  取消收藏成功这边改变一下样式
              ['pInfo[' + e.target.id + '].storeImg']: "/imgs/storedf.png",
            })
          }
        },
        fail: function () {
          console.log("收藏请求失败")
        }
      })
    } else {
      console.log("收藏请求没有openid")
      wx.switchTab({
        url: '/pages/mine/mine'
      })
    }
  },
})